package com.newer.weatherforecast.ui;

import java.util.ArrayList;
import java.util.List;

import com.newer.weatherforecast.dao.ConfigDAO;
import com.newer.weatherforecast.entity.MyConfig;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ConfigActivity extends Activity {
	private CheckBox cbAutoRefresh = null,cbAllowSms = null,cbSaveMsg = null,cbTips = null;
	private EditText etRefreshFreq = null, etKeyword = null;
	private GestureOverlayView gestureOverlayView = null;
	private RelativeLayout help = null,about = null;
	GestureLibrary gestureLibrary = null;
	boolean configIsChanged = false;
	ConfigDAO configDAO = null;
	AlertDialog alertDialog = null;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		init();
		if(!cbAutoRefresh.isChecked()){
			etRefreshFreq.setEnabled(false);
		}else{
			etRefreshFreq.setEnabled(true);
		}
		if(!cbAllowSms.isChecked()){
			cbSaveMsg.setChecked(false);
			cbSaveMsg.setEnabled(false);
			etKeyword.setEnabled(false);
		}else{
			cbSaveMsg.setEnabled(true);
			etKeyword.setEnabled(true);
		}
		cbAutoRefresh.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					etRefreshFreq.setEnabled(true);
				}else{
					etRefreshFreq.setEnabled(false);
				}
			}
		});
		cbAllowSms.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					cbSaveMsg.setChecked(true);
					cbSaveMsg.setEnabled(true);
					etKeyword.setEnabled(true);
				}else{
					cbSaveMsg.setChecked(false);
					cbSaveMsg.setEnabled(false);
					etKeyword.setEnabled(false);
				}
			}
		});
	}
	public void init(){
		cbAllowSms = (CheckBox) findViewById(R.id.cb_allow_sms);
		cbAutoRefresh = (CheckBox) findViewById(R.id.cb_autoRefresh);
		cbSaveMsg = (CheckBox) findViewById(R.id.cb_save_msg);
		etKeyword = (EditText) findViewById(R.id.et_keyword);
		etRefreshFreq = (EditText) findViewById(R.id.et_RefreshFreq);
		help = (RelativeLayout) findViewById(R.id.help);
		about = (RelativeLayout) findViewById(R.id.about);
		help.setOnClickListener(ocl);
		about.setOnClickListener(ocl);
		configDAO = new ConfigDAO(ConfigActivity.this);
		int type = getIntent().getExtras().getInt("type");
		if(type==1){
			cbAllowSms.setChecked(false);
			cbAutoRefresh.setChecked(false);
			cbSaveMsg.setChecked(false);
			etKeyword.setText("");
			etRefreshFreq.setText("");
		}else if(type==2){
			updateSettingInfo();
		}
	}
	OnClickListener ocl = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.help:
				AlertDialog alertDialog = new AlertDialog.Builder(ConfigActivity.this).create();
				alertDialog.setTitle("帮助");
				View helpView = View.inflate(ConfigActivity.this, R.layout.help, null);
				cbTips = (CheckBox) helpView.findViewById(R.id.cbTips);
				cbTips.setVisibility(View.GONE);
				alertDialog.setView(helpView);
				alertDialog.setButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				});
				alertDialog.show();
				break;
			case R.id.about:
				AlertDialog dialog = new AlertDialog.Builder(ConfigActivity.this).create();
				dialog.setTitle("关于");
				dialog.setMessage("110401MB" + "  第一小组 \n" + "组长:   " + "张鑫\n" + "组员:   " +"徐晓柏,彭毅,龙吉开,湛亮,李雪欣");
				dialog.setButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				});
				dialog.show();
				break;
			}
		}
	};
	public void updateSettingInfo(){
		MyConfig myConfig = configDAO.QueryMyConfig();
		cbAllowSms.setChecked(myConfig.isAllow_sms());
		cbAutoRefresh.setChecked(myConfig.isAutoRefresh());
		cbSaveMsg.setChecked(myConfig.isAllow_save_msg());
		etKeyword.setText(myConfig.getKeyword());
		etRefreshFreq.setText(myConfig.getRefreshFreq()+"");
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		MyConfig currentConfig = getUserSetting();
		MyConfig historyConfig = configDAO.QueryMyConfig();
		if(currentConfig.toString().equals(historyConfig.toString())){
			configIsChanged = false;
			return super.onKeyDown(keyCode, event);
		}else{
			configIsChanged = true;
		}
		if(keyCode==event.KEYCODE_BACK&&configIsChanged){
			userConfirm();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	public MyConfig getUserSetting(){
		MyConfig myConfig = new MyConfig();
		if(cbAutoRefresh.isChecked()){
			myConfig.setAutoRefresh(true);
		}else{
			myConfig.setAutoRefresh(false);
		}
		if(cbAllowSms.isChecked()){
			myConfig.setAllow_sms(true);
		}else{
			myConfig.setAllow_sms(false);
		}
		if(cbSaveMsg.isChecked()){
			myConfig.setAllow_save_msg(true);
		}else{
			myConfig.setAllow_save_msg(false);
		}
		myConfig.setKeyword(etKeyword.getText().toString());
		if(etRefreshFreq.getText().toString().equals("")){
			myConfig.setRefreshFreq(24);
		}else{
			myConfig.setRefreshFreq(Integer.parseInt(etRefreshFreq.getText().toString()));
		}
		return myConfig;
	}
	public void userConfirm(){
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("提示");
		alertDialog.setIcon(android.R.drawable.ic_menu_help);
		View view = View.inflate(ConfigActivity.this, R.layout.gestureconfirm, null);
		alertDialog.setView(view);
		gestureLibrary = GestureLibraries.fromRawResource(ConfigActivity.this, R.raw.gestures);
		gestureOverlayView = (GestureOverlayView) view.findViewById(R.id.gestureConfirm);
		gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {
			public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
				if(gestureLibrary.load()){
					List<Prediction> list = new ArrayList<Prediction>();
					list = gestureLibrary.recognize(gesture);
					System.out.println("开始监听" + list.size());
					alertDialog.dismiss();
					for (int i = 0; i < list.size(); i++) {
						System.out.println("进入循环");
						if(list.get(i).score>1.0){
							if(list.get(i).name.equals("confirm")){
								System.out.println("进入");
								boolean flag = configDAO.update(getUserSetting());
								if(flag){
									Toast.makeText(ConfigActivity.this, "更改已成功", Toast.LENGTH_SHORT).show();
									break;
								}else{
									Toast.makeText(ConfigActivity.this, "更改未成功", Toast.LENGTH_SHORT).show();
									break;
								}
							}else if(list.get(i).name.equals("cancel")||i==list.size()-1){
									Toast.makeText(ConfigActivity.this, "未进行更改", Toast.LENGTH_SHORT).show();
									break;
							}
						}
					}
					updateSettingInfo();
				}
			}
		});
		alertDialog.show();
	}
}
